package com.prs.crm.action.sale;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sale.GatheringWay;
import com.prs.crm.domain.sale.ShippingWay;
import com.prs.crm.service.sale.GatheringWayService;

public class GatheringWayAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private GatheringWay gatheringWay;

	private Integer[] typeIds;

	private GatheringWayService gatheringWayService;

	private PaginationSupport pagination;

	@Action("listGatheringWay")
	public String listGatheringWay() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				GatheringWay.class);
		this.setPagination(this.getGatheringWayService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editGatheringWay")
	public String editGatheringWay() {
		return SUCCESS;
	}

	@Action(value = "saveGatheringWay", results = { @Result(name = "success", type = "redirect", location = "listGatheringWay"),
			@Result(name="error",type="chain",location="editGatheringWay")})
	public String saveGatheringWay() {
		if(gatheringWay.getName() == null || gatheringWay.getName().equals("")) {
			this.addActionError("收款方式名称不能为空");
			return ERROR;
		}
		for(GatheringWay way:this.getGatheringWayService().getAllGatheringWay()){
			if(gatheringWay.getName().trim().equals(way.getName().trim())){
				this.addActionError("收款方式名称已存在");
				return ERROR;
			}
		}
		this.getGatheringWayService().save(gatheringWay);
		return SUCCESS;
	}

	@Action(value = "removeGatheringWay", results = { @Result(name = "success", type = "redirect", location = "listGatheringWay") })
	public String removeGatheringWay() {
		this.getGatheringWayService().batchRemove(typeIds);
		return SUCCESS;
	}

	
	public Integer[] getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}


	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public GatheringWay getGatheringWay() {
		return gatheringWay;
	}

	public void setGatheringWay(GatheringWay gatheringWay) {
		this.gatheringWay = gatheringWay;
	}

	public GatheringWayService getGatheringWayService() {
		return gatheringWayService;
	}

	public void setGatheringWayService(GatheringWayService gatheringWayService) {
		this.gatheringWayService = gatheringWayService;
	}

}
